package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.PartnerMapper;
import com.heima.pojo.Partner;
import com.heima.pojo.dto.PageQueryDTO;
import com.heima.pojo.vo.PartnerVo;
import com.heima.service.PartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PartnerServiceImpl implements PartnerService {

    @Autowired
    private PartnerMapper partnerMapper;
    @Override
    public Boolean save(Partner partner) {
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        int result = partnerMapper.insert(partner);
        return result != 0;
    }

    @Override
    public PageResult query(PageQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<PartnerVo> page = partnerMapper.queryParener(dto);
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(),page.getResult());
    }

    @Override
    public Partner findById(Integer id) {
        return partnerMapper.selectById(id);
    }

    @Override
    public Boolean update(Integer id, Partner partner) {
        partner.setUpdateTime(LocalDateTime.now());
        partner.setId(id);
        int result = partnerMapper.updateById(partner);
        return result != 0;
    }

    @Override
    public Boolean delete(Integer id) {
        int result = partnerMapper.deleteById(id);
        return result != 0;
    }

    @Override
    public Integer partnerCount() {
        return partnerMapper.partnerCount();
    }

    @Override
    public Boolean resetPwd(Integer id) {
        String password = Partner.INITIAL_PASSWORD;
        Integer result = partnerMapper.resetPwd(id,password);
        return result != 0;
    }

}
